package com.wenan.ui.controls.command
{
	import com.wenan.AppGlobal;
	import com.wenan.data.BrowsePageInfo;
	import com.wenan.data.ChangeButtonInfo;
	import com.wenan.data.PageInfo;
	import com.wenan.data.ParameterCollection;
	import com.wenan.data.PositionType;
	import com.wenan.ui.controls.menus.MenuItem;
	import com.wenan.ui.controls.menus.MenuItemEvent;
	import com.wenan.ui.controls.menus.PopupMenu;
	import com.wenan.ui.controls.standard.LinkLabel;
	import com.wenan.ui.homePages.collect.FavoritesView;
	import com.wenan.ui.homePages.mainShopBar.MainShopListPanel;
	import com.wenan.ui.layout.NavigatorPage;
	import com.wenan.ui.popup.PopupMenuManager;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import flashx.textLayout.container.ScrollPolicy;
	
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class RightCommandBar extends HBox
	{
		private var _page:NavigatorPage;
		private var statisticsButtonBar:StatisticsButtonBar;
		private var changeButtonBar:ChangeButton;
		private var wishlistReportButton:ImageButton;
		private var spaceCanvas:Canvas;
		private var wishlistPageLabel:ImageButton;
		
		public function RightCommandBar()
		{
			super();
			
			verticalScrollPolicy = ScrollPolicy.OFF;
			horizontalScrollPolicy = ScrollPolicy.OFF;
			
			setStyle("verticalAlign", "middle");
			setStyle("verticalGap", "0");
			
			initilizeComponents();		
		}
		
		public function get page():NavigatorPage
		{
			return _page;
		}
		
		public function set page(value:NavigatorPage):void
		{
			if(value)
			{
				_page = value;
				statisticsButtonBar.parentPageInfo = value.pageInfo;
			}
			
		}
		
		public function get statisticsReports():Array
		{
			return statisticsButtonBar.statisticsReports;
		}
		
		public function set statisticsReports(value:Array):void
		{
			statisticsButtonBar.statisticsReports = value;
		}
		
		public function get changeButton():PageInfo
		{
			return changeButtonBar.data as PageInfo;
		}
		
		public function set changeButton(value:PageInfo):void
		{
			if(value)
			{
				changeButtonBar.data = value;
				changeButtonBar.styleName = value.parameters.getByName("styleName").value;
				changeButtonBar.visible = true;
			}else
			{
				changeButtonBar.visible = false;
			}
		}
		
		
		/**
		 * 初始化控件。
		 */
		private function initilizeComponents():void
		{
			changeButtonBar = new ChangeButton();
			changeButtonBar.addEventListener(MouseEvent.CLICK, clickchangeButtonHandler);
			addElement(changeButtonBar);
			
			statisticsButtonBar = new StatisticsButtonBar();
			addElement(statisticsButtonBar);
			
			wishlistReportButton = new ImageButton();
			wishlistReportButton.source = "images/7.png";
			wishlistReportButton.toolTip = "用户收藏";
			wishlistReportButton.addEventListener(MouseEvent.CLICK, getCollectPage);
			addElement(wishlistReportButton);
			
//			spaceCanvas = new Canvas();
//			spaceCanvas.width = 10;
//			addElement(spaceCanvas);
			
			wishlistPageLabel = new ImageButton();
			wishlistPageLabel.source = "images/8.png";
			wishlistPageLabel.toolTip = "将本页设为首页";
			wishlistPageLabel.addEventListener(MouseEvent.CLICK, clickWishlistPageLabelHandler);
			addElement(wishlistPageLabel);
		}
		
		
		private function clickWishlistPageLabelHandler(event:MouseEvent):void
		{
			var home:SetHomePage=new SetHomePage();
			home.page = page;
			PopUpManager.addPopUp(home,page,true);
			PopUpManager.centerPopUp(home);
		}
		
		private function clickchangeButtonHandler(event:MouseEvent):void
		{
			var changeButton:ChangeButton = event.currentTarget as ChangeButton;
			var pageInfo:PageInfo = changeButton.data as PageInfo;
			
			if(pageInfo.positionType == PositionType.GLOBAL)
			{
				showMainUnionPanel(event);
			}
		}
		
		private function showMainUnionPanel(event:MouseEvent):void
		{
			var reference:UIComponent = event.target as UIComponent;
			AppGlobal.dataLayer.getMainShopUnion(resultHandler,faultHandler);
			
			function resultHandler(event:ResultEvent):void
			{
				var zldRow:MainShopListPanel = new MainShopListPanel();
				zldRow.dataProvider = event.result as Array;
				PopupMenuManager.popupAtControlMoving(zldRow,40, reference);
			}
			function faultHandler(evnet:FaultEvent):void
			{
				Alert.yesLabel = "确定"; 
				Alert.okLabel = "确定";
				
				Alert.show("统计信息获取失败！");
			}
		}
		
		private function showCollectPanel():void
		{
			
			
		}
		
		/**
		 * 获取用户收藏
		 */ 
		private function getCollectPage(event:MouseEvent):void
		{
			//获取用户收藏按钮控件
			var reference:UIComponent = event.currentTarget as UIComponent;
			
			//获取用户收藏记录
			AppGlobal.dataLayer.getFavorites(AppGlobal.userInfo.id,AppGlobal.userInfo.appkey, 10,resultHandler,faultHandler);
			
			function resultHandler(event:ResultEvent):void
			{
				var browsePageInfos:Array = event.result as Array;
				var favorites:FavoritesView = new FavoritesView();
				favorites.dataProvider = browsePageInfos;
				
				PopupMenuManager.popupAtControlMoving(favorites,160, reference);
			}
			
			function faultHandler(event:FaultEvent):void
			{
				
			}
		}
	}
}